package com.zcx.project.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcx.project.entity.ZcxLeave;
import com.zcx.project.entity.ZcxPlanDetail;
import com.zcx.project.entity.ZcxPlanDetailUser;
import com.zcx.project.entity.ZcxUser;
import com.zcx.project.mapper.ZcxLeaveMapper;
import com.zcx.project.mapper.ZcxPlanDetailMapper;
import com.zcx.project.mapper.ZcxPlanDetailUserMapper;
import com.zcx.project.mapper.ZcxUserMapper;


@Controller
@RequestMapping("/ZcxUser")
public class ZcxUserController {

    @Autowired
    ZcxUserMapper zcxUserMapper;

    @Autowired
    ZcxLeaveMapper zcxLeaveMapper;
    @Autowired
    ZcxPlanDetailUserMapper zcxPlanDetailUserMapper;
    @Autowired
    ZcxPlanDetailMapper zcxPlanDetailMapper;

    @RequestMapping(value = "ZcxUserHtml")
    public ModelAndView getMainPage(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxUser");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/findList")
    public List findList(ZcxUser obj) {
        List<ZcxUser> list = zcxUserMapper.findList(obj);
        // 是否有任务在身
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/findAble")
    public List findAble(ZcxUser obj) {
        List<ZcxUser> list = zcxUserMapper.findList(obj);
        // 判断是否在病假中
        List<ZcxLeave> zcxLeaves = zcxLeaveMapper.selectQJR();

        // 是否有任务在身

        ZcxPlanDetail zcxPlanDetail = zcxPlanDetailMapper.selectByPrimaryKey(obj.getPlanDetailId());
        ZcxPlanDetailUser zpu = new ZcxPlanDetailUser();
        zpu.setPlanStar(zcxPlanDetail.getPlanStar());
        zpu.setPlanEnd(zcxPlanDetail.getPlanEnd());
        List<ZcxPlanDetailUser> zcxPlanDetailUsers = zcxPlanDetailUserMapper.verify2(zpu);
        Set<Integer> bjr = zcxLeaves.stream().map(ZcxLeave::getUserId).collect(Collectors.toSet());
        Set<Integer> rwz = zcxPlanDetailUsers.stream().map(ZcxPlanDetailUser::getUserId).collect(Collectors.toSet());
        Iterator<ZcxUser> iterator = list.iterator();
        while (iterator.hasNext()) {
            ZcxUser next = iterator.next();
            if (bjr.contains(next.getId()) || rwz.contains(next.getId())) {
                iterator.remove();
            }
        }

        return list;
    }


    @ResponseBody
    @RequestMapping(value = "/get")
    public ZcxUser get(int id) {
        ZcxUser list = zcxUserMapper.selectByPrimaryKey(id);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Map<String, Object> findZcxUserPage(int currentPage, int rows, ZcxUser obj) {

        //使用分页插件,核心代码就这一行，页数、每页行数
        PageHelper.startPage(currentPage, rows);
        List<ZcxUser> list = zcxUserMapper.findList(obj);
        PageInfo<ZcxUser> pageInfo = new PageInfo<ZcxUser>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;


    }


    @ResponseBody
    @RequestMapping(value = "/insertZcxUser")
    public Map<String, Object> addZcxUser(ZcxUser obj) {

        ZcxUser login = zcxUserMapper.login(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (login == null) {
            int i = zcxUserMapper.insertSelective(obj);

            if (i == 0) {
                returnMap.put("code", "1001");
                returnMap.put("showInfo", "添加失败！");
            } else {
                returnMap.put("code", "2001");
                returnMap.put("showInfo", "添加成功！");
            }
        } else {
            returnMap.put("code", "3001");
            returnMap.put("showInfo", "用户名重复，添加失败！");
        }


        return returnMap;
    }


    @ResponseBody
    @RequestMapping(value = "/updateZcxUser")
    public Map<String, Object> updateZcxUser(ZcxUser obj) {
        int i = zcxUserMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else
            returnMap.put("showInfo", "修改成功！");
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteZcxUser")
    public Map<String, Object> deleteZcxUser(int id) {
        int i = zcxUserMapper.deleteByPrimaryKey(id);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "删除失败！");
        else
            returnMap.put("showInfo", "删除成功！");
        return returnMap;
    }


}
